<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

JFX::checkAdminPermsRedirect('core', 'perm_export_block');

$THEME->addBreadcrumb($this->adminUrl.'/export/export-block', $this->lang('ap_title_export_export-block'));

$BWT = JFX::registry('ThirdParty_BWT');
$MTF = JFX::registry('ThirdParty_MTF');


$blockid = $DB->escape(get('block'));

if($blockid != ''){
    if($DB->countRows($CONFIG->dbprefix.'blocks', "id = '{$blockid}' AND is_template = 1")==0){
        JFX::addError('Invalid block');
        JFX::redirect($CONFIG->adminUrl.'/export/export-block/');
    }

    // now have a valid block to export
    $filename = $CONFIG->tmpDir.'/block-export.'.$blockid.'.php';

    $this->exportBlock($blockid, $filename);

    while(ob_get_level()>0) ob_end_clean();
    header('Content-type: application/jfx-template');
    header('Content-disposition: attachment; filename='.$blockid.'.'.date('Ymd').'.jfxt');
    readfile($filename);
    exit;

}



// show the templates available to download

$templates = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}blocks WHERE is_template = 1 AND workspace_id = 1");

echo '<h1>'.$this->lang('export_template_heading').'</h1>';

if(count($templates)==0){
    echo '<p>'.$this->lang('error_no_templates').'</p>';
}else{
    echo '<ul>';
    foreach($templates as $k=>$v){
        echo '<li class="redirect"><a href="'.$CONFIG->adminUrl.'/export/export-block/?block='.$v['id'].'">'.JFX_Block::getTitleById($v['id']).'</a></li>';
    }
    echo '</ul>';
}








